package com.schoolET.controller.admin;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.schoolET.entity.Administrators;
import com.schoolET.entity.dto.AdminLoginDTO;
import com.schoolET.entity.vo.AdminEmailVO;
import com.schoolET.entity.vo.AdminLoginVO;
import com.schoolET.result.Result;
import com.schoolET.service.admin.IAdminLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author: lin
 * @data: 2024/5/29
 */
@Api(tags = "管理员登录")
@RestController
@RequestMapping("/schoolET")
public class LoginController {

    @Resource
    private IAdminLoginService adminLoginService;

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "管理员登录")
    @PostMapping("/login")
    public Result login(@Valid @RequestBody AdminLoginVO adminLoginVO) {
        AdminLoginDTO login = adminLoginService.login(adminLoginVO);
        //保存登录信息
        StpUtil.login(login.getId());
        //获取当前会话的token
        String tokenValue = StpUtil.getTokenValue();
        //设置token
        login.setToken(tokenValue);
        //保存角色和权限到login
        login.setRole(StpUtil.getRoleList());
        login.setPermission(StpUtil.getPermissionList());
        return Result.success(login).msg("登录成功");
    }

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "获取验证码")
    @PostMapping("/code")
    public Result getVerificationCode(@Valid @RequestBody AdminEmailVO adminEmailVO) {
        Administrators administrators = new Administrators();
        BeanUtil.copyProperties(adminEmailVO,administrators);
        adminLoginService.getVerificationCode(administrators);
        return Result.success().msg("验证码发送成功");

    }
}
